home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / macros.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  9KB  |  303 lines

  1. /* macros.c  routines to bind keyboard macros to individual keys */
  2.  
  3. #include <stdio.h>
  4. #include "ed.h"
  5.  
  6. #define FLEN 14
  7.  
  8. static FILE *mp;        /* file pointer for LOAD and SAVE */
  9. static char macfile[NFILEN+1] = "uemail.mcr";
  10. static short macstr[NKBDM];
  11.  
  12. typedef struct MACTAB {
  13.         struct  MACTAB  *m_fmac;        /* next MACTAB  */
  14.         short   *keymac;                /* pointer to the macro */
  15.         char    key;                    /* the key      */
  16. } MACTAB;
  17.  
  18. MACTAB  *mheadp = NULL; /* pointer to header structure */
  19.  
  20. /* GETMACRO  eXtended command to store a macro in the table above.
  21.  * Copies the current keyboard macro to the appropriate array.
  22.  * Bound to ^X-`.
  23.  */
  24. getmacro(f, n)
  25. int f,n;
  26. {
  27.         register int c,i;
  28.         short *maccpy();
  29.         register MACTAB *mt;
  30.         MACTAB *mcralloc();
  31.  
  32.         /* is there currently a macro being defined ? */
  33.         if (kbdmip != NULL || kbdmop != NULL) {
  34.                 mlwrite("Not now");
  35.                 return (FALSE);
  36.         }
  37.         mlwrite("Key to bind to current macro: ");
  38.         c = ttgetc();
  39.         ttputc(c);
  40.         if((mt=mcralloc(TRUE,c))==NULL)
  41.                 return(FALSE);
  42.         i = maclen(kbdm);
  43.         mt->key = c;                    /* install the key      */
  44.         if ((mt->keymac=(short *)malloc((i+1)*sizeof(short)))==NULL)
  45.                 {
  46.                 mlwrite("Cannot allocate space for macro");
  47.                 return(FALSE);
  48.                 }
  49.         maccpy(mt->keymac,kbdm);        /* copy the macro       */
  50.         return(TRUE);
  51. }
  52.  
  53. /* PUTMACRO  AGRAVE command finds macro associated with key returned
  54.  * by ttgetc and executes the macro.
  55.  */
  56.  
  57. putmacro(f, n)
  58. register int f, n;
  59. {
  60.         register int c;
  61.         short  *maccpy();
  62.         register MACTAB *mt;
  63.         MACTAB *mcralloc();
  64.  
  65.         c = ttgetc();   /* get next char */
  66.         switch(c)
  67.                 {
  68.                 case 0x07:      /* abort */
  69.                         return(ctrlg(FALSE,0));
  70.                 case 0x0c:      /* load a macro file */
  71.                         return(loadmac(TRUE));
  72.                 case 0x0d:      /* "make" a macro file */
  73.                         return(savemac());
  74.                 case 0x12:      /* incremental search back */
  75.                         return(backisearch(f, n));
  76.                 case 0x13:      /* incremental search forward */
  77.                         return(forwisearch(f, n));
  78.                 case 0x14:      /* get time */
  79.                         return(showtime(f,n));
  80.                 case '`':
  81.                         return(linsert(1,c));
  82.                 }
  83.         if (kbdmip!=NULL || kbdmop!=NULL)
  84.                 {
  85.                 mlwrite("Not now");
  86.                 return (FALSE);
  87.                 }
  88.         /* actually, there should be no way mheadp could != NULL and
  89.          * its forward pointer == NULL, but it's best to check
  90.          */
  91.         if (mheadp==NULL || mheadp->m_fmac == NULL)
  92.                 {
  93.                 mlwrite("No defineable macros assigned");
  94.                 return(FALSE);
  95.                 }
  96.         if((mt=mcralloc(FALSE,c))!= (MACTAB *)NULL)
  97.                 {
  98.                 /* save current macro to restore later */
  99.                 maccpy(macstr,kbdm);
  100.                 maccpy(kbdm,mt->keymac);
  101.                 c=ctlxe(f, n);          /* execute mac in keybrd.c */
  102.                 maccpy(kbdm,macstr);
  103.                 return(c);
  104.                 }
  105.         (*term.t_beep)();
  106.         return(FALSE);
  107. }
  108.  
  109. /* LOADMAC load a macro file and install the macros into memory.
  110.  * Bound to `^L.
  111.  */
  112. loadmac(f)
  113. int f;
  114. {
  115.         short *maccpy();
  116.         register int c,i;
  117.         register short s;
  118.         register MACTAB *mt;
  119.         MACTAB *mcralloc();
  120.  
  121.         if (f==TRUE)    /* FALSE == Called from main */
  122.                 {
  123.                 if(readpattern("Load macros: ",macfile)==ABORT)
  124.                         return(ABORT);
  125.                 }
  126.         else
  127.                 strcpy(macfile,"uemail.mcr");
  128.         if (macfile[0] == '~')
  129.                 parsefn(macfile);
  130.         if ((mp=fopen(macfile,"r"))==NULL)
  131.                 {
  132.                 mlwrite("Cannot open %s",macfile);
  133.                 return(FALSE);
  134.                 }
  135.         if ((s=getw(mp))!=(CTLX|'('))
  136.                 {
  137.                 mlwrite("Macro file format error: %s",macfile);
  138.                 fclose(mp);
  139.                 return(FALSE);
  140.                 }
  141.         while((c=fgetc(mp))!=EOF)
  142.                 {
  143.                 i=0;
  144.                 mlwrite("[Installing macro for key %c]",c);
  145.                 if((mt=mcralloc(TRUE,c))==NULL)
  146.                         {
  147.                         fclose(mp);
  148.                         return(FALSE);
  149.                         }
  150.                 while ((s=getw(mp))!=(CTLX|')'))
  151.                         {
  152.                         if (feof(mp))
  153.                                 {
  154.                                 mlwrite("Read error on: %s",macfile);
  155.                                 break;
  156.                                 }
  157.                         macstr[i++]=s;
  158.                         }
  159.                 macstr[i] = CTLX|')';
  160.                 mt->key = c;            /* install the key      */
  161.                 if ((mt->keymac=(short *)malloc((i+1)*sizeof(short)))==NULL)
  162.                         {
  163.                         fclose(mp);
  164.                         mlwrite("Cannot allocate space for macro");
  165.                         return(FALSE);
  166.                         }
  167.                 maccpy(mt->keymac,macstr); /* copy the macro    */
  168.                 }
  169.         fclose(mp);
  170.         return(TRUE);
  171. }
  172.  
  173. /* SAVEMAC function to save current keyboard macros to a file.
  174.  * Bound to `^M (makemac).
  175.  */
  176. savemac()
  177. {
  178.         short *maccpy();
  179.         register int i;
  180.         register short s;
  181.         register MACTAB *mt;
  182.  
  183.         if(readpattern("Save macros: ",macfile)==ABORT)
  184.                 return(ABORT);
  185.         if (macfile[0] == '~')
  186.                 parsefn(macfile);
  187.         if ((mp=fopen(macfile,"w"))==NULL)
  188.                 {
  189.                 mlwrite("Cannot open %s",macfile);
  190.                 return(FALSE);
  191.                 }
  192.         /* actually, there should be no way mheadp could != NULL and
  193.          * its forward pointer == NULL, but it's best to check
  194.          */
  195.         if (mheadp==NULL || mheadp->m_fmac == NULL)
  196.                 {
  197.                 mlwrite("No defineable macros assigned");
  198.                 return(FALSE);
  199.                 }
  200.         /* don't change the file format or the load function
  201.          * will fail
  202.          */
  203.         mt=mheadp->m_fmac;
  204.         putw(CTLX|'(',mp);
  205.         while(mt != NULL)
  206.                 {
  207.                 i=0;
  208.                 mlwrite("[Saving key: %c]",mt->key);
  209.                 maccpy(macstr,mt->keymac);
  210.                 fputc(mt->key,mp);
  211.                 while(macstr[i]!=(CTLX|')'))
  212.                         putw(macstr[i++],mp);
  213.                 putw(CTLX|')',mp);
  214.                 mt=mt->m_fmac;  /* next pointer */
  215.                 }
  216.         fclose(mp);
  217.         return(TRUE);
  218. }
  219.  
  220. /* find an existing keyboard macro or get space for a new one.  Return
  221.  * a pointer to the MACTAB structure so others can do with it what they
  222.  * will.
  223.  */
  224. MACTAB *
  225. mcralloc(f,c)
  226. register int f,c;
  227. {
  228.         register MACTAB *mt;
  229.  
  230.         if (mheadp == NULL)
  231.                 {
  232.                 if ((mheadp=(MACTAB *)malloc(sizeof(MACTAB))) == NULL)
  233.                         {
  234.                         mlwrite("Cannot allocate %d bytes",sizeof(MACTAB));
  235.                         return(FALSE);
  236.                         }
  237.                 mheadp->m_fmac = (MACTAB *)NULL;
  238.                 }
  239.         /* check to see if this one has already been allocated */
  240.         mt=mheadp;
  241.         while(mt->m_fmac != (MACTAB *)NULL)
  242.                 {
  243.                 if (mt->m_fmac->key == c)
  244.                         {
  245.                         if (f == TRUE)          /* reallocate old */
  246.                                 free(mt->m_fmac->keymac);
  247.                         return(mt->m_fmac);
  248.                         }
  249.                 mt=mt->m_fmac;  /* next pointer */
  250.                 }
  251.         /* this was a get request. return if no macro found */
  252.         if (f==FALSE)
  253.                 return(FALSE);
  254.         /*